<?php
/**
 * file todaysnewcontroller.php
 * Created on 2015-4-10 下午4:18:46 
 * Create by xiweijie
 */
namespace haibao\cms\business\control\pc\channel;

use haibao\cms\business\control\PublicController;
use haibao\cms\business\control\ArticleController;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;
use haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\business\control\pc\action\ArticleAction;
use haibao\cms\web\view\app\article\Preview;

class TodaysNewController extends ChannelController{

    const TRENDY_TAG = 12204;
    const STAR_TAG = 1383;
    const JEWELRYWATCH_TAG = 6863;
    const FASHION_TAG = 310;
    const LIFE_TAG = 520;
    const WEDDING_TAG = 211;
    const BEAUTY_TAG =  24;
    const BEAUTY_COLUMN_ONE = 1;
    const BEAUTY_COLUMN_FOUR = 4;
	const FEATURE_COUNT = 2;
    public function __construct($updateTime = null,$controlId,$isPublish){
        $this->savaLog();
        parent::__construct($updateTime,$controlId,$isPublish);
        $this->articleAction = ArticleAction::initArticleAction();
    }
    
    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);

        //今日最新
        $articleList = array();
        if($controlData['manual']){
            $articleList = $this->getActualData($controlData['manual']);
            $this->assign('articleList', $articleList);
        }
        
       
        $topTags = self::getTopTags();
        $topId = 6;
        $domainName = 'www';
        $channelUrl = $topTags[$topId]['channelurl'];
        if(is_array($this->conditionTagId)){
            $topId = current($this->conditionTagId);
        }else{
            $topId = $this->conditionTagId;
        }
        if(isset($controlData['manual']['tag_id']) && isset($topTags[$topId]['channelurl'])){
            $channelUrl = $topTags[$topId]['channelurl'];
        	$domainName = $topTags[$topId]['domain'];
        }
        
        $this->assign('moreUrl', $channelUrl.'/rightnow/'.$topId.'/');
        
        //精彩推荐
        $this->getSpecialInfo($controlData['auto'],$controlData['manual'],$topId);

        $this->assign('host', $channelUrl);
        $this->assign('picDomian', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC));

        $isNewHtml = $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        // 解析内容 更换链接
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    public function getActualData($dataCondition){
        $articleEntity = new ArticleEntity();
        $articleEntity->setLevel(4);
        $articleEntity->setBigLevel(7);
        $articleEntity->setLimit($dataCondition['limit']);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(false);
        $articleEntity->setIsConditionTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(true);
        $articleEntity->setTagIds($dataCondition['tag_id']);
        $articleEntity->setOrderByNew(true,'desc');
        $articleEntity->setSource(__CLASS__);
        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(true);
        $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FIVE);
        $this->parseTagId($dataCondition['tag_id'],$dataCondition['tag_id']);
        $this->articleAction->initArticleAction();
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$imageColls) = $this->articleAction->getListForNew();
        $mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
        $articleList = array();
        foreach($mergeData as $key=>$article){
            $articleArr = $article->toArray();
            $articleArr['SiteType'] = $article->SiteType;
            
            if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){	
            	
            	$tag = $articles['tag'];
            	$articleTag = $articles['articleTag'];
            
            	$articleArr['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
            	$articleArr['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
            	$articleArr['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
            	$articleArr['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
            	
            	$articleArr['ProImage'] =  isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE]:
            	(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');

            }else{
            	
            	$articleArr['PublishId'] = $article->Id;
            	$tag = $imageColls['tag'];
            	$collTag = $imageColls['collTag'];
            	
            	$articleArr['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
            	$articleArr['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
            	$articleArr['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
            	$articleArr['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
            	$articleArr['ProImage'] =  isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE]:
            	(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
            }
            
            /* if(!isset($articleArr['image'])) $articleArr['image'] = array();
            $temp = array();
            $temp['ProImage'] = $articleArr['ProImage'];
            array_push($articleArr['image'],$temp); */
            
            array_push($articleList,$articleArr);
        }
        $articleList =  json_decode(json_encode($articleList));
        return $articleList;
    }
    
   /**
    * 获取今日最新右侧
    */
    private function getSpecialInfo($multiData,$manualData,$topId){
    	$featureList = array();
    	$manualData['multiDataType'] = self::DATA_TYPE_FEATURE.','.self::DATA_TYPE_IMAGE_SET.','.self::DATA_TYPE_ARTICLE.','.self::DATA_TYPE_IMAGE_EXCLUSIVE;
    	$manualData['proLevel'] = \haibao\cms\model\data\ArticleProImage::Level_FIVE;
    	
	    $this->parseMultiData($multiData,$manualData);
	    //取手动数据
	    $entityType = $entityTypes = array();
	    if($this->packageArr){
	        foreach($this->packageArr as $arr){
	            $entity = $arr['Entity'];
	            $dataEntity = array();
	            if($arr['Type'] == self::DATA_TYPE_FEATURE){
	                $dataEntity['Url'] = $entity->Url;
	                $dataEntity['Pic'] = $entity->Pic;
	                $dataEntity['Title'] = $entity->FeatureDefine->FeatureName;
	                $entityType['Id'] = $entity->Id;
	                $entityType['Type'] = self::DATA_TYPE_FEATURE;
	            }elseif($arr['Type'] == self::DATA_TYPE_IMAGE_EXCLUSIVE){
	                $dataEntity['Url'] = $entity->ExclusiveUrl;
	                $dataEntity['Pic'] = $entity->PicUrl;
	                $dataEntity['Title'] = $entity->ExclusiveName;
	                $entityType['Id'] = $entity->Id;
	                $entityType['Type'] = self::DATA_TYPE_IMAGE_EXCLUSIVE;
	            }elseif($arr['Type'] == self::DATA_TYPE_IMAGE_SET){
                    $dataEntity['Url'] = self::getArticleUrl($entity->Id,$entity->ImageCollectiveTag->ParentId,2);
                    $dataEntity['Pic'] = $entity->ImageCollectivePro;
                    $dataEntity['Title'] = $entity->Title;
                    $entityType['Id'] = $entity->Id;
                    $entityType['Type'] = self::DATA_TYPE_IMAGE_SET;
	            }else{
	                $dataEntity['Url'] = self::getArticleUrl($entity->PublishId,$entity->ArticleTag->ParentId);
	                $dataEntity['Pic'] = $entity->ArticleProImage;
	                $dataEntity['Title'] = $entity->Title;
	                $entityType['Id'] = $entity->Id;
	                $entityType['Type'] = self::DATA_TYPE_ARTICLE;
	                $entityType['TagId'] = $entity->ArticleTag->TagId;
	            }
	            array_push($featureList,$dataEntity);
	            $entityTypes[] = $entityType;
	        }
	    }
    	//手动数据 
    	if($this->packageArrCount < 3){
    	    $articleId = $handArticleId = $diffArr = $articleInfo = $returnArticleInfo = array();
    	    $articleInfoList = $this->getSpecialArticle($topId);
    	    if($this->packageArrCount > 0){
    	       foreach($this->packageArr as $k=>$v){
    	           if($v['Entity']->SiteType == 1){
    	               $handArticleId[] = $v['Entity']->Id;//手动文章ID
    	           }
    	       }    
    	    }
    	    if($handArticleId && $articleInfoList){
    	        foreach($articleInfoList as $key=>$val){
    	             foreach($handArticleId as $k=>$v){
    	                 if($v != $val['Id']){
    	                     $articleInfo[] = $articleInfoList[$key];//手动数据中有文章的情况下  去重后的文章
    	                 }
    	             }
    	        }
    	    }
    	    if($articleInfo){
    	        $returnArticleInfo = array_slice($articleInfo,0,3-$this->packageArrCount);
    	    }else{
    	        $returnArticleInfo = array_slice($articleInfoList,0,3-$this->packageArrCount);
    	    }
    	    $featureList = array_merge($featureList,$returnArticleInfo);
    	}
    	
    	$this->assign('featureList', $featureList);
    }
    
    public function orderByTime($exclusive,$num){
        foreach ($exclusive as $riqi) {
            $time[] = $riqi['PublishRiqi'];
        }
        array_multisort($time, SORT_DESC, $exclusive);
        return array_slice($exclusive,0,$num,true);
    }
    
   

    /**
     * 获取自动数据
     * @param : $dataCondition 数据条件
     */
    public function getSpecialArticle($topId){
        $articleEntity = new ArticleEntity();
        $articleEntity->setLevel(3);
        $articleEntity->setBigLevel(7);
        $articleEntity->setLimit(3);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(false);
        $articleEntity->setIsConditionTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(false);
        $articleEntity->setSource(__CLASS__);
        $articleEntity->setTagIds($topId);
        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(true);
        $articleEntity->setOrderByClick(true);
        $articleEntity->setOrderByNew(false);
        $articleEntity->setIsDate(true, \haibao\cms\business\control\pc\action\ArticleAction::DATE_TYPE_HALFWEEK);
        $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FIVE);
        $this->articleAction->initArticleAction();
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$articleProImages,$tagInfo,$articleTagArr) = $this->articleAction->getListForNew();
        $articleList = array();
        $articleInfo = array();
        $this->tagInfo = $tagInfo;
        foreach($articles as $key=>$article){
            $articleList['Id'] = $article->Id;
            $articleList['Title'] = (isset($article->ShortTitle) && !empty($article->ShortTitle)) ? $article->ShortTitle : $article->Title;
            $articleList['Url'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
            $articleList['PublishRiqi'] = $article->PublishTime->format('Y-m-d H:i:s');
            if(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])){
                $articleList['Pic'] = $articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE];
            }else{
                if(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
                    $articleList['Pic'] = $articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
                } else {
                    $articleList['Pic'] = \haibao\cms\business\control\PublicController::getArticleFirstImage($article->Id);
                }
            }
            array_push($articleInfo,$articleList);
        }
        return $articleInfo;
    }
}